import java.io.File;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Properties;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class SendMail2 {

	final static String from = "Vincell <info@vincell.hu>";

	final static Properties properties = System.getProperties();

	static {
		properties.setProperty("mail.smtp.host", "localhost");
		properties.setProperty("mail.user", "");
		properties.setProperty("mail.password", "");

//		properties.setProperty("proxySet", "true");
//		properties.setProperty("socksProxyHost", "10.137.1.16");
//		properties.setProperty("socksProxyPort", "1080");
//	
//		properties.setProperty("java.net.socks.username", "osz-domosija");
//		properties.setProperty("java.net.socks.password", "domike08");
				
        Authenticator.setDefault(new SendMail2.ProxyAuth("osz-domosija", "domike08"));
	}
	
	public static class ProxyAuth extends Authenticator {
	    private PasswordAuthentication auth;

	    private ProxyAuth(String user, String password) {
	        auth = new PasswordAuthentication(user, password == null ? new char[]{} : password.toCharArray());
	    }

	    protected PasswordAuthentication getPasswordAuthentication() {
	        return auth;
	    }
	}

	public static void createMail(String to, String name, String trackId, Notification notif)
			throws EmailException {
		HtmlEmail he = new HtmlEmail();
		String htmlMessage = notif.getHtmlMessage();
		htmlMessage = htmlMessage.replace("{{name}}", name);
		htmlMessage = htmlMessage.replace("{{trackId}}", trackId);
		he.setHtmlMsg(htmlMessage);

		if (htmlMessage.contains("cid:logo")) {
			File logo = new File("logo.gif");
			// PNGDataSource png = new PNGDataSource(decodedPNGOutputStream); //
			// a
			// custom class
			he.embed(logo, "logo");
		}

		String textMessage = notif.getTextMessage();
		if (textMessage != null) {
			textMessage = textMessage.replace("{{name}}", name);
			textMessage = textMessage.replace("{{trackId}}", trackId);
			he.setTextMsg(textMessage);
		}

		he.setFrom("info@vincell.hu");
		he.addTo(to);
		he.send();

		// // Get the default Session object.
		// final Session session = Session.getDefaultInstance(properties);
		//
		// try {
		// final MimeMultipart multipart = new MimeMultipart("related");
		// BodyPart messageBodyPart = new MimeBodyPart();
		// // String htmlText = notif.getHtmlMessage().replaceAll("",
		// // replacement)
		// messageBodyPart.setContent(notif.getHtmlMessage(), "text/html");
		// multipart.addBodyPart(messageBodyPart);
		//
		// if (notif.getHtmlMessage().contains("cid:logo")) {
		// BodyPart imagePart = new MimeBodyPart();
		// DataSource fds = new FileDataSource("logo.gif");
		// imagePart.setDataHandler(new DataHandler(fds));
		// imagePart.setHeader("Content-ID", "<logo>");
		//
		// // add it
		// multipart.addBodyPart(imagePart);
		// }
		//
		// final MimeMessage message = new MimeMessage(session);
		// message.setFrom(new InternetAddress(from));
		// message.addRecipient(Message.RecipientType.TO, new InternetAddress(
		// to));
		// message.setSubject(notif.getSubject()/* , "utf-8" */);
		// // message.setText(notif.getTextMessage()/*, "utf-8"*/);
		// message.setContent(multipart);
		//
		// // Send message
		// Transport.send(message);
		// System.out.println("ok");
		//
		// } catch (final MessagingException mex) {
		// mex.printStackTrace();
		// }
	}
}
